const RuleCRUDLayoutVue = {
    returnVue() {
        window.RuleCRUDLayoutVue = new Vue({
            el: "#ruleCRUDLayout",
            data: {
                modelList: {
                    single: "单个",
                    batch: "批量"
                },
                ruleEditBox: "",//规则编辑框内容
                model: "single",
                isSingleShow: true,//是否对的单个相关按钮进行显示处理
                isBatchShow: false,//是否对批量相关按钮进行显示处理
                ruleKeyList: {
                    userNameArr: {name: "用户名黑名单模式(精确匹配)", size: 0},
                    userNameKeyArr: {name: "用户名黑名单模式(模糊匹配)", size: 0},
                    userUIDArr: {name: "用户uid黑名单模式(精确匹配)", size: 0},
                    userWhiteUIDArr: {name: "用户uid白名单模式(精确匹配)", size: 0},
                    titleKeyArr: {name: "标题黑名单模式(模糊匹配)", size: 0},
                    titleKeyCanonicalArr: {name: "标题黑名单模式(正则匹配)", size: 0},
                    commentOnKeyArr: {name: "评论关键词黑名单模式(模糊匹配)", size: 0},
                    contentOnKeyCanonicalArr: {name: "评论关键词黑名单模式(正则匹配)", size: 0},
                },
                defaultSelect: "userUIDArr",//当前下拉框选中的值
                outRuleSelect: "allRuleOutFIle",
                outRUleModelList: {
                    allRuleOutFIle: "全部规则到文件",
                    allRuleOutShearPlate: "全部规则到剪贴板",
                    allUIDRuleOutFIle: "全部UID规则到文件",
                    barrageShieldingRule: "b站弹幕屏蔽规则"
                },
                inputRuleSelect: "从下面编辑框导入全部规则",
                inputEditContent: "",
                inoutRUleModelList: ["从下面编辑框导入全部规则", "从下面编辑框合并导入UID规则"],
                isInputEditShow: true
            },
            methods: {
                add() {
                    const selectRUleItem = this.getSelectRUleItem();
                    if (selectRUleItem.ruleName === undefined || selectRUleItem.ruleName === null) {
                        Tip.error('出现了意外的类型bug:155532');
                        return;
                    }
                    UrleCrud.addShow(selectRUleItem.ruleType, selectRUleItem.ruleName)
                },
                addAll() {
                    const selectRUleItem = this.getSelectRUleItem();
                    if (selectRUleItem.ruleName === undefined || selectRUleItem.ruleName === null) {
                        Tip.error('出现了意外的类型bug:155533');
                        return;
                    }
                    const content = this.ruleEditBox;
                    if (content === null) return;
                    if (content === "") {
                        Tip.error("请输入正确的内容！");
                        return;
                    }
                    UrleCrud.addAllShow(selectRUleItem.ruleType, selectRUleItem.ruleName, content);
                },
                delAll() {
                    const list = this.ruleKeyList;
                    let str = "";
                    for (let key in list) {
                        const name = list[key].name;
                        const size = Util.getData(key, []).length;
                        str += `规则名:${name} 个数:${size}个\n`;
                    }
                    if (!confirm(`是要全部规则吗？，以下是您的全部规则基本信息\n\n${str}`)) {
                        return;
                    }
                    const okData = {success: 0, fail: 0};
                    for (const key in this.ruleKeyList) {
                        if (Util.delData(key)) {
                            okData.success++;
                        } else {
                            okData.fail++;
                        }
                    }
                    this.updateRuleIndex();
                    alert(`删除结果:\n成功:${okData.success}\n失败:${okData.fail}`);
                },
                delItem() {
                    const selectRUleItem = this.getSelectRUleItem();
                    if (selectRUleItem.ruleName === undefined || selectRUleItem.ruleName === null) {
                        Tip.error('出现了意外的类型bug:155535');
                        return;
                    }
                    UrleCrud.delItemShow(selectRUleItem.ruleType, selectRUleItem.ruleName);
                },
                delKey() {
                    const selectRUleItem = this.getSelectRUleItem();
                    if (selectRUleItem.ruleName === undefined || selectRUleItem.ruleName === null) {
                        Tip.error('出现了意外的类型bug:155537');
                        return;
                    }
                    UrleCrud.delShow(selectRUleItem.ruleType, selectRUleItem.ruleName);
                },
                findKey() {
                    const selectRUleItem = this.getSelectRUleItem();
                    if (selectRUleItem.ruleName === undefined || selectRUleItem.ruleName === null) {
                        Tip.error('出现了意外的类型bug:155536');
                        return;
                    }
                    UrleCrud.findKeyShow(selectRUleItem.ruleType, selectRUleItem.ruleName);
                },
                setKey() {
                    const selectRUleItem = this.getSelectRUleItem();
                    if (selectRUleItem.ruleName === undefined || selectRUleItem.ruleName === null) {
                        Tip.error('出现了意外的类型bug:155537');
                        return;
                    }
                    UrleCrud.setKeyShow(selectRUleItem.ruleType, selectRUleItem.ruleName);
                },
                getSelectRUleItem() {//返回defaultSelect中选中的规则项
                    const ruleType = this.defaultSelect;
                    const ruleName = this.ruleKeyList[ruleType].name;
                    return {ruleType: ruleType, ruleName: ruleName}
                },
                updateRuleIndex() {//更新规则的个数
                    const tempList = this.ruleKeyList;
                    for (let item in tempList) {
                        const tempSize = tempList[item].size;
                        const newSize = Util.getData(item, []).length;
                        if (tempSize === newSize) {
                            continue;
                        }
                        tempList[item].size = newSize;
                    }
                },
                getOutRuleDataFormat(space = 3) {//获取导出规则的结果内容
                    const ruleKeyList = this.ruleKeyList;
                    const data = {};
                    for (let key in ruleKeyList) {
                        const ruleName = ruleKeyList[key].name;
                        data[ruleName] = Util.getData(key, []);
                    }
                    return JSON.stringify(data, null, space);
                },
                inputRuleLocalData(json) {//导入规则内容！
                    const list = this.ruleKeyList;
                    for (let ruleKey in list) {
                        const name = list[ruleKey].name;
                        const jsonRuleList = json[name];
                        if (!jsonRuleList) {
                            continue;
                        }
                        if (jsonRuleList.length === 0) {
                            continue;
                        }
                        Util.setData(ruleKey, jsonRuleList);
                    }
                    this.updateRuleIndex();
                    alert("已导入");
                },
                outRule() {
                    const outType = this.outRUleModelList[this.outRuleSelect];
                    switch (outType) {
                        case "全部规则到文件":
                            let fileName = "规则-" + Util.toTimeString();
                            const s = prompt("保存为", fileName);
                            if (s === null) return;
                            if (!(s.includes(" ") || s === "" || s.length === 0)) fileName = s;
                            Util.fileDownload(this.getOutRuleDataFormat(), fileName + ".json");
                            break;
                        case "全部规则到剪贴板":
                            Util.copyToClip(this.getOutRuleDataFormat(0));
                            break;
                    }
                },
                inputRule() {
                },
                lookLocalRUleContent() {
                    Util.openWindowWriteContent(this.getOutRuleDataFormat(3));
                },
                lookLocalAppointRUleContent() {
                    const item = this.getSelectRUleItem();
                    if (!confirm(`是要查询${item.ruleName}的规则内容吗？`)) return;
                    const data = Util.getData(item.ruleType, []);
                    if (data.length === 0) {
                        Tip.info(`${item.ruleName}规则内容为空的！`);
                        return;
                    }
                    Util.openWindowWriteContent(JSON.stringify(data, null, 3));
                }
            },
            watch: {
                model(newVal, oldVal) {
                    if (newVal === oldVal) return;
                    if (newVal === "single") {
                        this.isBatchShow = false;
                        this.isSingleShow = true;
                    } else {
                        this.isBatchShow = true;
                        this.isSingleShow = false;
                    }
                }
            },
        });
    }
}